using Game.Framework.Localization;
using Game.Framework.Utils;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Scripts.Install
{
    public class UIInstall : MonoBehaviour 
    {
        private static UIInstall _ui;

        private Slider _slider;

        public static void Open()
        {
            var canvas = GameObject.Find("CanvasWidth").GetComponent<Canvas>();
            var root = canvas.transform.Find("Root");

            var isPad = Util.IsLE_16_10();
#if UNITY_ANDROID
            var prefab = Resources.Load<GameObject>(isPad ? "UI/UILaunching/Prefabs/UILaunching_Pad" : "UI/UILaunching/Prefabs/UILaunching");
#else
            var prefab = Resources.Load<GameObject>(isPad ? "UI/UILaunching/Prefabs/UILaunchingIOS_Pad" : "UI/UILaunching/Prefabs/UILaunchingIOS");
#endif

            var ui = Object.Instantiate(prefab, root);
            _ui = ui.AddComponent<UIInstall>();
        }

        public static void Close()
        {
            Destroy(_ui.gameObject);
            _ui = null;
        }

        private LocalizeTextMeshProUGUI _loadingText;

        private void Awake()
        {
            _loadingText = transform.Find("Root/BottomGroup/SliderLoading/TextLoad").GetComponent<LocalizeTextMeshProUGUI>();

            _slider = transform.Find("Root/BottomGroup/SliderLoading").GetComponent<Slider>();

            // EventManager.Instance.Subscribe(this);
        }

        private void OnDestroy()
        {
            // EventManager.Instance.Unsubscribe(this);
        }

        private float _deltaTime;

        private void FixedUpdate()
        {
            _deltaTime += Time.deltaTime;

            var value = Mathf.Lerp(0, 0.7f, _deltaTime / 10f);
            _slider.value = value;

            var str = Mathf.Floor(_slider.value * 100);
            string strValue = $"{str} %";
            _loadingText.SetText(strValue);

            // _loadingText.SetTerm("UI_loading_load", ((int)(_slider.value * 100)).ToString());
        }

        // public void OnNotify(DiskFullEvent message)
        // {
        //     UIInstallTips.Open("Oops, your phone storage is full, please clean up and try again.", "UI_common_button_yes", null);
        // }
    }
}